const baseUrl = 'https://api.9hkj.net/capi'
const header = {
	Authorization: ""
}
const request = (url = '', type = 'post', date = {}) => {
	const userinfo = uni.getStorageSync('userinfokey')
	header.Authorization = userinfo.token
	return new Promise((resolve, reject) => {
		uni.request({
			method: type,
			url: baseUrl + url,
			data: date,
			header: header,
			dataType: 'json',
		}).then((response) => {
			if(response.data.code==2003){
				uni.showModal({
					title: '登录提示',
					content: '您的登录已过期,请重新登录!!!',
					showCancel:false,
					success: function(res) {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/homepage/myhome'
							});
							uni.removeStorageSync('userinfokey');
						} 
					}
				});
			}
			resolve(response);
		}).catch(error => {
			// console.log(error);
			reject(error)
		})
	});
}

const uploadimg = (filePath) => {
	const userinfo = uni.getStorageSync('userinfokey')
	header.Authorization = userinfo.token
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: 'https://api.9hkj.net/capi/upload/image',
			filePath: filePath,
			header: {
				Authorization: header.Authorization
			},
			name: 'file', // 后台接收文件的字段名
			success: (res) => {
				const url = JSON.parse(res.data).data;
				resolve(url); // 将上传文件的URL传递给Promise对象
			},
			fail: (err) => {
				// console.log(res);
				reject(err);
			}
		});
	});
}

export default {
	request,
	uploadimg
};